package com.jiayuge.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 功能: Swagger的配置类 将swagger添加到项目中  --层
 * <p>Swagger的配置类 将swagger添加到项目中  --层</p >
 * <ul>
 * <li>数据库: 增 删 改 查 </li>
 * <li>{@link SwaggerConfig} connect to {@link SwaggerConfig}</li>
 * </ul>
 *
 * @Program: SpringBoot-Swagger
 * @Date: 2021-09-26 11:14
 * @Version 1.0
 * @Author Jiayu.Yang
 */

@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {

    //配置wsagger的bean实例
    @Bean
    public Docket docket(Environment environment){

        //发布版本中一般要关闭swagger
        //判断当前环境是否为dev 和test
        Profiles profile = Profiles.of("dev","test");
        //条件满足返回true
        boolean flag = environment.acceptsProfiles(profile);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag) //swager开关
                .select()

                /*
                RequestHandlerSelectors 配置扫描接口的方式
                    any()扫描所有
                    none()不扫描任何包
                    basePackage()指定要扫描的包
                    。。。

                 */
                .apis(RequestHandlerSelectors.any())

                /*
                配置过滤路劲
                 */
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {

        return new ApiInfoBuilder()
                .title("Swagger使用")
                .description("每天掌握一个小技术")
                .version("v1.0")
                .build();
    }
}
